import { defineEventHandler, getQuery } from 'h3';
import { useResponseSuccess } from '#imports';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 模拟统计数据
  const stats = {
    total: 156,
    available: 89,
    occupied: 45,
    maintenance: 12,
    retired: 10,
    by_type: {
      PHYSICAL: 45,
      HUMAN: 23,
      DIGITAL: 18,
      SERVICE: 35,
      BUSINESS: 20,
      TECHNICAL: 15,
    },
    by_status: {
      ACTIVE: 134,
      MAINTENANCE: 12,
      RETIRED: 10,
    },
    by_category: {
      1: 25, // 物理资源
      11: 15, // 房间资源
      12: 10, // 设备资源
      2: 15, // 人力资源
      21: 15, // 员工资源
      3: 18, // 数字资源
      4: 35, // 服务资源
      5: 20, // 业务资源
      6: 15, // 技术资源
    },
  };

  // 如果指定了分类ID，返回该分类的统计
  if (query.category_id) {
    const categoryId = parseInt(query.category_id as string);
    return {
      code: 0,
      data: {
        total: stats.by_category[categoryId] || 0,
        available: Math.floor((stats.by_category[categoryId] || 0) * 0.6),
        occupied: Math.floor((stats.by_category[categoryId] || 0) * 0.3),
        maintenance: Math.floor((stats.by_category[categoryId] || 0) * 0.1),
      },
      msg: '获取分类统计成功',
    };
  }

  return {
    code: 0,
    data: stats,
    msg: '获取资源统计成功',
  };
});
